package com.touchstone.cloudpacs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.touchstone.cloudpacs.common.utils.CommonUtils;
import com.touchstone.cloudpacs.common.utils.SqlUtils;
import com.touchstone.cloudpacs.common.utils.request.PageRequest;
import com.touchstone.cloudpacs.entity.OperateLog;
import com.touchstone.cloudpacs.mapper.OperateLogMapper;
import com.touchstone.cloudpacs.request.query.LogPageQuery;
import com.touchstone.cloudpacs.service.IOperateLogService;
import com.touchstone.cloudpacs.vo.OperateLogPageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description :
 * @Author : hunter
 * @Date: 2019-07-11 23:07
 * @Version: 1.0
 **/
@Slf4j
@Service
public class OperateLogServiceImpl implements IOperateLogService {

    @Autowired
    private OperateLogMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(OperateLog operateLog) {
        operateLog.setId(CommonUtils.getUuid());
        operateLog.setOperateTime(new Date());
        int record = mapper.save(operateLog);
        if (record > 0) {
            log.info("成功插入操作日志：{}", operateLog);
        } else {
            log.info("未插入日志: {}", operateLog);
        }
        return record;
    }

    @Override
    public PageInfo<OperateLogPageVO> selectByPage(PageRequest<LogPageQuery> pageRequest) {
        log.info("分页查询操作日志，查询条件：{} ", pageRequest);
        if (CommonUtils.isNotEmpty(pageRequest.getParam())) {
            //处理模糊查询中可能出现的特殊符号
            pageRequest.getParam().setUserName(SqlUtils.handleKeywordSpecialCharacter(pageRequest.getParam().getUserName()));
            pageRequest.getParam().setContent(SqlUtils.handleKeywordSpecialCharacter(pageRequest.getParam().getContent()));
        }
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit(), pageRequest.getOrderBys());
        return new PageInfo<>(mapper.selectByQuery(pageRequest.getParam()));
    }
}
